// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Duan Lian (duanlian01@baidu.com)
//
// Description: OTA Coap Downloader

#include "baidu_iot_ota_coap_downloader.h"
#include "string.h"
#include "stdlib.h"
#include "heap_monitor.h"
#include "iot-baidu-ca/include/baidu_ca.h"

namespace duer {

OTACoapDownloader::OTACoapDownloader(URL* url, const char* cert) :
      _m_p_url(url),
      _m_p_coaps_client(NULL) {
}

OTACoapDownloader::~OTACoapDownloader() {
}

int OTACoapDownloader::connect() {
    int ret = 1;

    if (!_m_p_coaps_client) {
        if (_m_p_url->valid()) {
            // Parse url to extract scheme, domain, port, path, etc
            const char* host = _m_p_url->host();
            const char* ip = NULL;
            _m_p_coaps_client = NEW(OTA) CoapsClient(ip,
                                                     _m_p_url->port(),
                                                     _m_p_url->path(),
                                                     0);
            _m_p_coaps_client->connect();

            ret = 0;
        }
    }

    return ret;
}

int OTACoapDownloader::get(data_available_cb cb, void* ctx) {

    return 0;
}

int OTACoapDownloader::disconnect() {
    _m_p_coaps_client->disconnect();

    return 0;
}

} // namespace duer
